package leetcode_500;

/**
 *@author 周杨
 *MagicalString_481 输出一个魔法字符串里前N个里面1的数量
 *describe:先构造魔法字符串 很无聊 AC 45 双指针
 *2018年8月31日 下午5:49:56
 */
public class MagicalString_481 {
	public int magicalString(int n) {
        if (n < 3) {
           return n == 0 ? 0 : 1;
       }
       int res = 1;
       int[] chars = new int[n];
       chars[0] = 1;
       chars[1] = 2;
       chars[2] = 2;
       int index = 3;
       for (int i = 2; i < n && index < n; i++) {
           chars[index] = 3 - chars[index - 1];
           res += 2 - chars[index++];
           if (chars[i] == 2 && index < n) {
               chars[index] = chars[index - 1];
               res += 2 - chars[index++];
           } 
       }
       return res;
   }
}
